package com.zc.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web项目跨域config配置
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

/*    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    *//**
     * 配置不需要拦截的请求
     * @param registry
     *//*
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns = new String[] {"/adminLogin","/product/**","/bigType/**","/user/wxlogin","/weixinpay/**"};
        registry.addInterceptor(new SysInterceptor())
                .addPathPatterns("/**")//针对所有
                .excludePathPatterns(patterns);//排除
    }

    *//**
     * 允许跨域（前后端分离项目）
     * @param registry
     *//*
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对所有的 HTTP 请求路径进行 CORS （跨域资源共享）验证
                .allowedOriginPatterns("*") // 对所有请求来源允许
                .allowCredentials(true) // 允许跨源请求中携带证书，比如 cookies，http,ssl认证
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS") // 支持所有请求
                .maxAge(3600); // 最大有效时间
    }*/


    /**
     * 请求图片路径映射重定向
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/img/**")
//                .addResourceLocations("file:D:\\userImages\\blog\\img\\");// 如果访问image路径，就映射到本地D盘
                .addResourceLocations("http://sv1q2b3uo.hn-bkt.clouddn.com/img/");// 如果访问image路径，就映射到七牛云
        /*registry.addResourceHandler("/image/**")
                .addResourceLocations("file:C:\\userImages\\");// 虚拟*/
    }

}
